package com.fangxuele.tool.push.ui.form;

import com.fangxuele.tool.push.App;
import com.fangxuele.tool.push.dao.TAccountMapper;
import com.fangxuele.tool.push.dao.TPeopleMapper;
import com.fangxuele.tool.push.domain.TAccount;
import com.fangxuele.tool.push.domain.TPeople;
import com.fangxuele.tool.push.util.JTableUtil;
import com.fangxuele.tool.push.util.MybatisUtil;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.google.common.collect.Maps;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import lombok.Getter;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * AccountManageForm
 * </pre>
 *
 * @author <a href="https://github.com/rememberber">RememBerBer</a>
 * @since 2021/3/10.
 */
@Getter
public class PeopleManageForm {

    private JPanel peopleManagePanel;
    private JTable peopleListTable;
    private JButton deleteButton;
    private JButton createPeopleButton;
    private JComboBox accountComboBox;

    private static PeopleManageForm peopleManageForm;

    public static Map<String, Integer> accountMap;

    private static TAccountMapper accountMapper = MybatisUtil.getSqlSession().getMapper(TAccountMapper.class);
    private static TPeopleMapper peopleMapper = MybatisUtil.getSqlSession().getMapper(TPeopleMapper.class);

    private PeopleManageForm() {
    }

    public static PeopleManageForm getInstance() {
        if (peopleManageForm == null) {
            peopleManageForm = new PeopleManageForm();
        }
        return peopleManageForm;
    }

    /**
     * 初始化消息列表
     */
    public static void init() {
        peopleManageForm = getInstance();
        peopleManageForm.getDeleteButton().setIcon(new FlatSVGIcon("icon/remove.svg"));
        peopleManageForm.getCreatePeopleButton().setIcon(new FlatSVGIcon("icon/add.svg"));

        initAccountComboBox();
        initPeopleList();
    }

    private static void initAccountComboBox() {
        accountMap = Maps.newHashMap();
        peopleManageForm.getAccountComboBox().removeAllItems();
        int msgType = App.config.getMsgType();
        List<TAccount> tAccountList = accountMapper.selectByMsgType(msgType);
        for (TAccount tAccount : tAccountList) {
            String accountName = tAccount.getAccountName();
            Integer accountId = tAccount.getId();
            peopleManageForm.getAccountComboBox().addItem(accountName);
            accountMap.put(accountName, accountId);
        }
    }

    public static void initPeopleList() {
        // 人群列表
        String[] headerNames = {"人群名称", "id"};
        DefaultTableModel model = new DefaultTableModel(null, headerNames);
        peopleManageForm.getPeopleListTable().setModel(model);
        // 隐藏表头
        JTableUtil.hideTableHeader(peopleManageForm.getPeopleListTable());

        int msgType = App.config.getMsgType();
        String selectedAccountName = (String) peopleManageForm.getAccountComboBox().getSelectedItem();
        Integer selectedAccountId = accountMap.get(selectedAccountName);

        Object[] data;

        List<TPeople> tPeopleList = peopleMapper.selectByMsgTypeAndAccountId(String.valueOf(msgType), selectedAccountId);
        for (TPeople tPeople : tPeopleList) {
            data = new Object[2];
            data[0] = tPeople.getPeopleName();
            data[1] = tPeople.getId();
            model.addRow(data);
        }
        // 隐藏id列
        JTableUtil.hideColumn(peopleManageForm.getPeopleListTable(), 1);

        PeopleEditForm.clearAll();
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        peopleManagePanel = new JPanel();
        peopleManagePanel.setLayout(new GridLayoutManager(3, 1, new Insets(10, 10, 10, 0), -1, -1));
        peopleManagePanel.setMaximumSize(new Dimension(-1, -1));
        peopleManagePanel.setMinimumSize(new Dimension(-1, -1));
        peopleManagePanel.setPreferredSize(new Dimension(280, -1));
        panel1.add(peopleManagePanel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        final JScrollPane scrollPane1 = new JScrollPane();
        peopleManagePanel.add(scrollPane1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        peopleListTable = new JTable();
        peopleListTable.setGridColor(new Color(-12236470));
        peopleListTable.setRowHeight(36);
        peopleListTable.setShowVerticalLines(false);
        scrollPane1.setViewportView(peopleListTable);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 3, new Insets(0, 0, 0, 0), -1, -1));
        peopleManagePanel.add(panel2, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        deleteButton = new JButton();
        deleteButton.setText("删除");
        panel2.add(deleteButton, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        panel2.add(spacer1, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        createPeopleButton = new JButton();
        createPeopleButton.setEnabled(true);
        createPeopleButton.setText("新建");
        panel2.add(createPeopleButton, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        accountComboBox = new JComboBox();
        peopleManagePanel.add(accountComboBox, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    }
}
